package com.isfto.service.impl;


import com.isfto.dao.ApueDao;
import com.isfto.dao.BeforeFlightChangeEnterAanOutDao;
import com.isfto.dao.FlightChangeEnterDao;
import com.isfto.entity.tablepojo.Apue;
import com.isfto.entity.tablepojo.FlightChange;
import com.isfto.entity.tablepojo.PageBean;
import com.isfto.service.FlightChangeEnterService;
import com.isfto.service.FlightChangeOutService;
import com.isfto.service.FlightInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlightChangeEnterServiceImpl implements FlightChangeEnterService {
    @Resource
    FlightChangeEnterDao flightChangeDao;

    @Resource
    FlightChangeOutService flightChangeOutService;

    @Resource
    BeforeFlightChangeEnterAanOutDao beforeFlightChangeEnterAanOutDao;

    @Resource
    FlightInfoService flightInfoService;

    @Resource
    ApueDao apueDao;
    @Override
    public int saveInfo(FlightChange flightChange) {
        return flightChangeDao.saveInfo(flightChange);
    }

    @Override
    public PageBean flightChange(Integer page, Integer pageSize, Integer flag) {
        if(flag!=1){
            PageBean pageBean = flightChangeOutService.flightChange(page, pageSize);
            return  pageBean;
        }else {
            int total = flightChangeDao.total();
            Integer start = (page - 1) * pageSize;
            List<FlightChange> flightChanges = flightChangeDao.flightChange(start, pageSize);
            PageBean pageBean = new PageBean(total, flightChanges);
            return pageBean;
        }
    }

    @Override
    public int updateStauts(FlightChange flightChange) {

        return flightChangeDao.updateStauts(flightChange);
    }

    @Override
    public FlightChange beforeInfoEnter(FlightChange flight) {
        FlightChange flightChange = flightChangeDao.beforeInfoEnter(flight.getCfno(), flight.getStartapcd(),flight.getEndapcd(),flight.getStartfptt(),flight.getEndfplt());
        List<FlightChange> flightChanges = beforeFlightChangeEnterAanOutDao.beforeInfoEnter(flight.getCfno(), flight.getStartapcd(),flight.getEndapcd());
        //更新之后的开始时间值
        String upstime= flightChange.getStartfptt();
        //更新之前开始时间值
        String bfstime= flightChanges.get(0).getStartfptt();
        Apue startName = apueDao.startName(flight.getStartapcd());
        flightInfoService.updateInfoStart(upstime,startName.getFlightCode(),bfstime);
        //更新之后到达时间值
        String upetime=flightChange.getEndfplt();
        //更新之前到达时间值
        String bfetime=flightChanges.get(0).getEndfplt();
        Apue endName = apueDao.endName(flight.getEndapcd());
        flightInfoService.updateInfoEnd(upetime,endName.getFlightCode(),bfetime);
        return null;
    }

    @Override
    public FlightChange changeBeforeInfoEnter(FlightChange flight) {
        FlightChange flightChange = flightChangeDao.beforeInfoEnter(flight.getCfno(), flight.getStartapcd(),flight.getEndapcd(),flight.getStartfptt(),flight.getEndfplt());
        List<FlightChange> flightChanges = beforeFlightChangeEnterAanOutDao.beforeInfoEnter(flight.getCfno(), flight.getStartapcd(),flight.getEndapcd());
        //更新之后的开始时间值
        String upstime= flightChange.getStartfptt();
        //更新之前开始时间值
        String bfstime= flightChanges.get(0).getStartfptt();
        Apue startName = apueDao.startName(flight.getStartapcd());
        flightInfoService.updateInfoStart(bfstime,startName.getFlightCode(),upstime);
        //更新之后到达时间值
        String upetime=flightChange.getEndfplt();
        //更新之前到达时间值
        String bfetime=flightChanges.get(0).getEndfplt();
        Apue endName = apueDao.endName(flight.getEndapcd());
        flightInfoService.updateInfoEnd(bfetime,endName.getFlightCode(),upetime);
        return null;
    }

    @Override
    public PageBean flightChangeInfo(String startapcd, String endapcd, String startfptt, String endfplt, Integer page, Integer pageSize,Integer flag) {
        if(flag!=1){
            PageBean pageBean = flightChangeOutService.flightChangeInfoOUt(startapcd, endapcd, startfptt, endfplt, page, pageSize);
            return pageBean;
        }else{
            Integer total = flightChangeDao.flightChangeCount(startapcd, endapcd, startfptt, endfplt);
            Integer  start=(page-1)*pageSize;
            List<FlightChange> flightChanges = flightChangeDao.flightChangeInfo(startapcd, endapcd, startfptt, endfplt,start,pageSize);
            PageBean pageBean=new PageBean(total,flightChanges);
            return pageBean;
        }
    }

}
